/*
        Firesoft 
        
    Copyright (C) 2010, Arttu Viljakainen 
    Published under GNU General Public License V3 
    (see http://www.gnu.org/licenses/licenses.html)
    
    This file is part of Firesoft.

    Firesoft is free software: 
    you can redistribute it and/or modify it under the terms of the GNU General 
    Public License as published by the Free Software Foundation, 
    either version 3 of the License, or (at your option) any later version.

    Firesoft is distributed in the hope that it will be useful, 
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 
    Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tool for Path Finding Algorithm Testing. If not, see         
    <http://www.gnu.org/licenses/>.
 */
package softstarter.gui.subareas.commandline;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import softstarter.gui.master.GUIStateController;
import softstarter.gui.subareas.ButtonFactory;
import softstarter.gui.subareas.FireActionsController;
import softstarter.gui.subareas.LaunchActionListener;
import softstarter.launching.RunnableItem;

public class CommandLine {

    private GUIStateController GUIController;
    
	private JPanel masterContainer;
	
	private JComboBox commandLine;
	private JButton goButton;

	private CommandlineTypingListener commandlineTypingListener;
	private FireActionsController fireActionsController;
	
	private LaunchActionListener launchActionListener;
	
	public CommandLine(GUIStateController GUIController, FireActionsController controller) {
		this.GUIController = GUIController;
		fireActionsController = controller;
	    build();
	}
	
	private void build() {
		masterContainer = new JPanel();
	    FlowLayout layout = new FlowLayout();
	    layout.setHgap(0);
	    layout.setVgap(0);
	    masterContainer.setLayout(layout);
//		masterContainer.setBorder(BorderFactory.createTitledBorder("Run"));
//		masterContainer.setBackground(Color.WHITE);
		commandLine = new JComboBox();
		
		commandLine.setEditable(true);
		commandLine.setPreferredSize(new Dimension(350, 25));
		
		goButton = new JButton("Go");
						
		commandlineTypingListener = new CommandlineTypingListener(fireActionsController);
		launchActionListener = new LaunchActionListener(fireActionsController);
		masterContainer.setBorder(new EmptyBorder(0, 0,0, 0));
		commandLine.getEditor().getEditorComponent().addKeyListener(commandlineTypingListener);
		goButton.addActionListener(launchActionListener);
		
		masterContainer.add(new ButtonFactory().getImageLabel("./resources/Logo.png"));
		masterContainer.add(commandLine);
//		masterContainer.add(goButton);
	}
	
	public void populateCommandLineAndShowPopup(List<RunnableItem> topHits) {
		
		String firstElement = commandLine.getEditor().getItem().toString();
		commandLine.removeAllItems();
		
		for(RunnableItem file: topHits) {
			System.out.println("--->" + file.getName());
			commandLine.addItem(file);
		}
		
		commandLine.getEditor().setItem(firstElement);

		commandLine.revalidate();
		
		commandLine.showPopup();
		
		GUIController.refreshGUI();
	}
	
	public String getSearchWord() {
	    return commandLine.getEditor().getItem().toString();
	}
	
	public String getFirstLaunchCandidate() {
	    return commandLine.getSelectedItem().toString();
	}
	
	public String getSecondLaunchCandidate() {
	    RunnableItem item = ((RunnableItem)commandLine.getItemAt(0));
	    if(item != null)
	        return item.getFullPath();
	    else
	        return ""; 
	}
	
	public JComboBox getCommandLine() {
		return commandLine;
	}
	
	public JButton getGoButton() {
		return goButton;
	}
	
	public JPanel getMasterContainer() {
		return masterContainer;
	}
	
	public void clearTextArea() {
        commandLine.setSelectedItem("");
	}
	
	public void setFocusToCommandLine() {
	    commandLine.getEditor().getEditorComponent().requestFocusInWindow();
	}
}
